﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.Xml.Linq;
using System.IO;
/*
 * Adanti Luca 
 * 4Bi
 * 21/3/2012
 * */

namespace Adanti.Luca._4BI.LeggiXML
{
    /// <summary>
    /// Logica di interazione per MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnLeggi_Click(object sender, RoutedEventArgs e)
        {
            int nElementiChannel = 0, nElementiCategory = 0;
            try
            {
                //OpenFileDialog dialog = new OpenFileDialog();
                //dialog.ShowDialog();
                //XElement X = XElement.Load(dialog.FileName);

                XElement X = XElement.Load("http://www.ilsole24ore.com/rss/tecnologie.xml");

                var Canali = X.Elements("channel");
                nElementiChannel = Canali.Count();

                foreach (var canale in Canali)
                {
                    //XElement elemento = new XElement(tmp.Name, tmp.Elements());

                    if (canale.HasElements)
                    {
                        var ElementiCategory = canale.Elements("category");
                        nElementiCategory = nElementiCategory + ElementiCategory.Count();
                        XElement categorie = new XElement("categorie", ElementiCategory.Elements());
                        lblCategory.Content = "Numero Elementi \"Category\":" + nElementiCategory;

                        int cont = 1;
                        foreach (var categoria in ElementiCategory)
                        {
                            txtFileXML.Text = txtFileXML.Text + "category" + cont + ";";
                            cont++;
                        }

                        XElement ElementoItem = canale.Element("item");
                        var title = ElementoItem.Element("title");
                        lblTitle.Content = "Title:" + title.Value;
                        var description = ElementoItem.Element("description");
                        lblDescription.Content = "Description:" + description.Value;
                    }
                }
                lblNElementi.Content = "Numero Elementi \"Channel\":" + nElementiChannel;
            }
            catch
            {
                MessageBox.Show("Percorso del file errato", "ERRORE",MessageBoxButton.OK,MessageBoxImage.Error); 
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
            
                //XElement X = XElement.Load("http://www.ilsole24ore.com/rss/tecnologie.xml");
                XElement X = XElement.Load("http://www.newsrimini.it/rss/homepage.xml");

                //var articoli = XElement articolo in X.Element("channel").Elements("item");

                //foreach (XElement articolo in X.Element("channel").Elements("item"))
                var items = from XElement item in X.Element("channel").Elements("item")
                            
                            where item.Element("title").Value.ToLower().Contains("harry")
                            orderby item.Element("title").Value
                            
                            select item;

                foreach (var item in items)
                {
                    Console.WriteLine( item.Value );
                }

                
                // Visualizzare una stringa CSV (Category1, Category2, Category3 … ) 
                // cumulando il valore degli elementi “category” (attenzione: category è dentro ad un elemento channel)

                var categorie = from XElement item in X.Element("channel").Elements("category")
                                select item;

                string result = "";
                foreach (var item in categorie)
                {
                    result += item.Value + ";";
                }


            }
            catch { }

            XElement X1 = XElement.Load("http://www.ilsole24ore.com/rss/tecnologie.xml");
            var itis = from XElement item in X1.Elements("channel")
                       select item;

            int quantiSono = itis.Count();

        }
    }
}
